///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.logger {
	import flash.events.StatusEvent;
	import flash.events.AsyncErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.LocalConnection;
	import mx.logging.LogEvent;
	import mx.logging.targets.LineFormattedTarget;
	import mx.utils.ObjectUtil;
	
	/**
	 * Target for the Flex Logging framework.
	 */
	public class LogMessageDispatcherTarget extends LineFormattedTarget {
		private var _conn:LocalConnection;
		
		public function LogMessageDispatcherTarget() {
			super();
			_conn = new LocalConnection();
            _conn.addEventListener(StatusEvent.STATUS, onStatus);
            _conn.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
            _conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		}
		
		public override function logEvent(event:LogEvent):void {
			_conn.send("_echo1","update",{level:event.level,category:event.currentTarget.category,message:event.message,time:this.includeTime});
		}
		
		private function onStatus(evt:StatusEvent):void {
			//trace("onStatus Event: "+ObjectUtil.toString(evt));
		}
		
		private function onAsyncError(evt:AsyncErrorEvent):void {
			trace("onAsyncError Event: "+ObjectUtil.toString(evt));
		}
		
		private function onSecurityError(evt:SecurityErrorEvent):void {
			trace("onSecurityError Event: "+ObjectUtil.toString(evt));
		}
	}
}